package com.cloudxingkong.dianyinke.NewTeacher.Utils;

import android.content.Context;
import android.location.LocationManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

public class HttpUtils {
	public static byte[] request(String uString){
		ByteArrayOutputStream baos=new ByteArrayOutputStream();
		
		try {
			URL url=new URL(uString);
			HttpURLConnection connection=(HttpURLConnection) url.openConnection();
			connection.setConnectTimeout(10000);
			connection.setReadTimeout(10000);
			connection.connect();
			if (connection.getResponseCode()==200) {
				InputStream iStream=connection.getInputStream();
				byte b[]=new byte[1024];
				int len=0;
				while ((len=iStream.read(b))!=-1) {
					baos.write(b, 0, len);
				}
				return baos.toByteArray();
			}
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static boolean isNetConnected(Context context) {
		if (context != null) {
			ConnectivityManager cm= (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
			NetworkInfo info = cm.getActiveNetworkInfo();
			if (info != null) {
				return info.isAvailable();
			}
		}
		return false;
	}

	/**
	 * 检测GPS是否打开
	 */
	public static boolean isGPSEnabled(Context context){
		LocationManager lm= (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
		List<String> list = lm.getProviders(true);
		for (String s : list) {
			if ("gps".equals(s)){
				return true;
			}
		}
		return false;
	}
}
